<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">


      <el-form-item label="允许注册">
        <el-radio v-model="form.reg_ok" label="1">是</el-radio>
        <el-radio v-model="form.reg_ok" label="2">否</el-radio>
      </el-form-item>

      <upload :label="'用户默认头像'" :old_path="form.path" @upload-event="success"></upload>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

<script>
    import { getUserConfig,setUserConfig } from '@/api/user'
    import Upload from '@/components/Upload'

    export default {
        components: { Upload },
        
        data() {
            return {
                upload_url:'',
                upload_token:'',
                dataObj: { token: '' },
                imageUrl: '',
                form:{
                    'path':'',
                    'reg_ok':'1'
                }
            }
        },
        created() {
            this.getUserConfig();
        },
        methods: {

            //  图片上传
            success(value){
                this.form.path = value;
            },
            getUserConfig(){
                return new Promise((resolve, reject) => {
                    getUserConfig().then(response => {

                        this.form = response.data;
                        this.imageUrl = response.data;

                        resolve(true)
                    }).catch(err => {

                        reject(false)
                    })
                });

            },


            onSubmit() {
                setUserConfig(this.form).then(response => {
                    if (response.status == 'success'){
                        this.$message({
                            message: '设置成功',
                            type: 'success'
                        })
                    }
                }).catch(err => {

                })
            },


            handleAvatarSuccess(res, file) {
                // this.form.path = res.hash;
                this.imageUrl = URL.createObjectURL(file.raw);


                var a = res.replace("hash=","");
                a = a.replace(/"/g,'');
                this.form.path = a;
            },
        }
    }
</script>

